package com.xing.fragment;

import java.util.UUID;

import com.xing.controller.DBController;
import com.xing.questionnairesurvey.R;
import com.xing.util.CurrentUserUtil;
import com.xing.util.DateUtil;
import com.xing.util.ViewUtil;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class CreateHsdDataResearchFragment extends SuperFragment {

	private String  name,age,phone;
	/**
	 * sex 1 male
	 * 2  female
	 */
	private int sex;
	private EditText nameEditText,ageEditText,phoneEditText;
	private RadioGroup sexvGroup;
	private Button createButton;
	private String surveyuuid;
	private FragmentManager fragmentManager;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		fragmentManager = getFragmentManager();
		return (view = inflater.inflate(R.layout.create_hsd_data_research,container,false));
	}
	
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		surveyuuid = this.getArguments().getString("surveyuuid");
		initWidgets();
	}
	
	
	private void initWidgets(){
		nameEditText = (EditText) view.findViewById(R.id.research_name);
		ageEditText = (EditText) view.findViewById(R.id.research_age);
		phoneEditText = (EditText) view.findViewById(R.id.research_phone);
		sexvGroup = (RadioGroup) view.findViewById(R.id.sex_radiogroup);
		createButton = (Button) view.findViewById(R.id.create_hsd_btn);
		view.findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				fragmentManager.popBackStack();
			}
		});
		sexvGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				int id = sexvGroup.getCheckedRadioButtonId();
				RadioButton currentRadioButton = (RadioButton) view.findViewById(id);
				String sexsString = currentRadioButton.getText().toString().trim();
				if (sexsString.trim().equals("男")) {
					sex = 1;
				}else {
					sex = 2;
				}
			}
		});
		
		createButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				
				int documenttype = 1;
				
				String idnumber = "";
				String researchaccount ="";
				String researchname = CurrentUserUtil.userName;
				
				String createtime = DateUtil.currentDateTime();
				String modifytime = DateUtil.currentDateTime();
				String researchuuid = UUID.randomUUID().toString();
				
				name = nameEditText.getText().toString().trim();
				age = ageEditText.getText().toString().trim();
				phone = phoneEditText.getText().toString().trim();
				
				
				if ( ! (TextUtils.isEmpty(name) | TextUtils.isEmpty(age) | TextUtils.isEmpty(phone) | TextUtils.isEmpty("sex"))) {
					DBController.getInstance().insertHsdDataResearch(getActivity(),researchuuid,surveyuuid, name,sex, Integer.parseInt(age), "",phone, "", documenttype, idnumber, researchaccount, researchname, createtime, modifytime, createQuestionaireHandler);
				}else {
					ViewUtil.showToast(getActivity(), "信息不可为空");
				}
			}
		});
	
	}
	
	private Handler createQuestionaireHandler =  new Handler(){
		public void handleMessage(android.os.Message msg) {
			boolean isInsert = (Boolean) msg.obj;
			if (isInsert) {
				ViewUtil.showToast(getActivity(), "新建问卷成功");
				fragmentManager.popBackStack();
			}else {
				ViewUtil.showToast(getActivity(), "新建问卷失败");
			}
			
		}
	};
	
}
